#!/bin/bash
#
# fxrestheart      This shell script takes care of starting and stopping
#               fxrestheart (on 8006)
#
#
# chkconfig: 2345 99 20

# Source function library.
. /etc/init.d/functions
HOSTNAME=`hostname -f`

getpid() {
  echo `ps -ef|grep -w $prog|grep -v grep|awk '{printf $2}'`
}

FXRESTHEART_DIR=/opt/restheart/restheart
PIDFILE=/var/run/restheart.pid
prog=restheart
#PORT=8005
PID=`getpid`

if [ -z $PID ] && [ -e ${PIDFILE} ]; then
  rm ${PIDFILE}
fi

if [ -z $PID ] && [ -e /var/lock/subsys/$prog ]; then
  rm -f /var/lock/subsys/$prog
fi

start() {
echo begin to start
            PID=`getpid`

            if [ $PID ]; then
                echo Foxit Restheart Server already running.
        else
                cd $FXRESTHEART_DIR
                java -Xmx1024M -Xms512M -server -jar restheart.jar etc/restheart.yml >> /var/log/fxrestheart.log 2>&1 &
                RETVAL=$?
                if [ $RETVAL -eq 0 ]; then
                  action $"Starting $prog: " /bin/true
                  touch /var/lock/subsys/$prog
                else
                  action $"Starting $prog: " /bin/false
                fi
                return $RETVAL
        fi

}

stop() {
        PID=`getpid`
        if [ -z $PID ]; then
          echo "Foxit Restheart Server already stopped"
        else
          kill $PID
          action $"Stopping $prog: " /bin/true
        fi
        rm -f /var/lock/subsys/$prog ${PIDFILE}
}

case "$1" in
    start)
        start
        ;;
    stop)
        stop
        ;;
    status)
        status -p ${PIDFILE} $prog
        ;;
    restart)
        stop
        sleep 5
        start
        ;;
    *)
        echo "Usage:  {start|stop|status|restart}"
        exit 1
        ;;
esac
exit $?

